package com.romotc.qqrobot.webqq.bean;

import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

import com.romotc.json.tools.Jackson;

/**
 * http://d.web2.qq.com/channel/poll2 请求返回信息对应Bean //过滤掉空消息
 * {"retcode":102,"errmsg":""} // 真实消息
 * {"retcode":0,"result":[{"poll_type":"message","value":{"msg_id":11028,"from_uin":1670433408,"to_uin":1586030808,"msg_id2":589200,"msg_type":9,"reply_ip":176752342,"time":1326529150,"content":[["font",{"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"}],"ddd
 * "]}}]}
 * 
 * @author Administrator
 * 
 */
public class Poll2_ResponseBody {

	private int retcode;
	private String errmsg;
	private String p;
	private String t;
	private List<Poll2_ResponseBody_Result> result;

	/**
	 * 
	 * @param responseStr
	 * @return
	 * @throws Exception
	 
	public static Poll2_ResponseBody parseFromJsonStr(String responseStr)
			throws Exception {
		if (StringUtils.isBlank(responseStr))
			return null;

		JsonFactory f = new JsonFactory();
		JsonParser jp = f.createJsonParser(responseStr);
		Poll2_ResponseBody body = new Poll2_ResponseBody();
		jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
		while (jp.nextToken() != JsonToken.END_OBJECT) {
			String fieldname = jp.getCurrentName();
			jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY

			if ("result".equals(fieldname)) { // contains an object
				// START_ARRAY
				List<Poll2_ResponseBody_Result> results = new ArrayList<Poll2_ResponseBody_Result>();
				while (jp.nextToken() != JsonToken.END_ARRAY) {
					// START_OBJECT
					Poll2_ResponseBody_Result result = new Poll2_ResponseBody_Result();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						fieldname = jp.getCurrentName();

						jp.nextToken(); // move to value, or
										// START_OBJECT/START_ARRAY
						if ("poll_type".equals(fieldname)) {
							result.setPoll_type(jp.getText());
						} else if ("value".equals(fieldname)) {
							Poll2_ResponseBody_Result_Value value = new Poll2_ResponseBody_Result_Value();
							// jp.nextToken(); // will return
							// JsonToken.START_OBJECT (verify?)
							while (jp.nextToken() != JsonToken.END_OBJECT) {
								fieldname = jp.getCurrentName();
								jp.nextToken(); // move to value, or
												// START_OBJECT/START_ARRAY

								if ("content".equals(fieldname)) { // contains
																	// an object
									// content的内容只能通过手工解析了
									// [
									// ["font",
									// {"size":13,
									// "color":"000000",
									// "style":[0,0,0],
									// "name":"\u9ED1\u4F53"
									// }
									// ],
									// "ddd "
									// ]
									// START_ARRAY
									String valueStr = null;
									while (jp.nextToken() != JsonToken.END_ARRAY) {
										// START_ARRAY
										while (jp.nextToken() != JsonToken.END_ARRAY) {
											ContentFont font = new ContentFont();
											value.setFont(font);
											while (jp.nextToken() != JsonToken.END_OBJECT) {
												fieldname = jp.getCurrentName();
												jp.nextToken();
												if ("size".equals(fieldname)) {
													font.setSize(jp.getText());
												} else if ("color"
														.equals(fieldname)) {
													font.setColor(jp.getText());
												} else if ("style"
														.equals(fieldname)) {
													// jp.nextToken();
													int[] style = new int[3];
													style[0] = jp.getIntValue();
													jp.nextToken();
													style[1] = jp.getIntValue();
													jp.nextToken();
													style[2] = jp.getIntValue();
													font.setStyle(style);
													jp.nextToken();
												} else if ("name"
														.equals(fieldname)) {
													font.setName(jp.getText());
												}

											}
										}

										jp.nextToken();
										String ssString = jp.getText();
										value.setContentMsg(ssString);

									}

								} else if ("msg_id".equals(fieldname)) {
									value.setMsg_id(jp.getLongValue());

								} else if ("from_uin".equals(fieldname)) {
									value.setFrom_uin(jp.getLongValue());

								} else if ("to_uin".equals(fieldname)) {
									value.setTo_uin(jp.getLongValue());

								} else if ("msg_id2".equals(fieldname)) {
									value.setMsg_id2(jp.getLongValue());
								} else if ("msg_type".equals(fieldname)) {
									value.setMsg_type(jp.getIntValue());
								} else if ("reply_ip".equals(fieldname)) {
									value.setReply_ip(jp.getLongValue());
								} else if ("time".equals(fieldname)) {
									value.setTime(jp.getLongValue());
								} else if ("mode".equals(fieldname)) {
									value.setMode(jp.getText());
								} else if ("type".equals(fieldname)) {
									value.setType(jp.getIntValue());
								} else if ("name".equals(fieldname)) {
									value.setName(jp.getText());
								} else if ("session_id".equals(fieldname)) {
									value.setSession_id(jp.getIntValue());
								} else if ("inet_ip".equals(fieldname)) {
									value.setInet_ip(jp.getLongValue());
								} else if ("cancel_type".equals(fieldname)) {
									value.setCancel_type(jp.getIntValue());
								} else {
									throw new IllegalStateException(
											"Unrecognized field '" + fieldname);
								}
							}

							// result.setValue(value);
						}

					}
					results.add(result);
				}
				body
						.setResult(results
								.toArray(new Poll2_ResponseBody_Result[results
										.size()]));
			} else if ("retcode".equals(fieldname)) {
				body.setRetcode(jp.getIntValue());

			} else if ("errmsg".equals(fieldname)) {
				body.setErrmsg(jp.getText());

			} else {
				throw new IllegalStateException("Unrecognized field '"
						+ fieldname);
			}
		}
		jp.close(); // ensure resources get cleaned up timely and properly

		return body;
	}*/

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public int getRetcode() {
		return retcode;
	}

	public void setRetcode(int retcode) {
		this.retcode = retcode;
	}

	public String getErrmsg() {
		return errmsg;
	}

	public void setErrmsg(String errmsg) {
		this.errmsg = errmsg;
	}


	public List<Poll2_ResponseBody_Result> getResult() {
		return result;
	}

	public void setResult(List<Poll2_ResponseBody_Result> result) {
		this.result = result;
	}

	public static Poll2_ResponseBody parseFromJsonStr(String resBody) throws Exception{
		ObjectMapper mp = Jackson.getObjectMaper();
		return mp.readValue(resBody, Poll2_ResponseBody.class);
	}

	@Override
	public String toString() {
		try {
			StringBuilder bld = new StringBuilder();
//			return Jackson.getObjectMaper().writeValueAsString(this);
			/**
{"retcode":0,"errmsg":null,"p":null,"t":null,
			"result":[{"poll_type":"message","value":{"session_id":0,"msg_type":9,"inet_ip":0,"type":0,"from_uin":-26,"mode":null,"content":[["font","{\"name\":\"黑体\",\"size\":\"13\",\"style\":[0,0,0],\"color\":\"000000\"}"],"哈哈 明天下午聚餐 "],"lc_id":0,"cancel_type":0,"time":1329016787406,"msg_id2":591628,"name":null,"msg_id":16003,"to_uin":2,"reply_ip":176498388}}]}


{"retcode":0,"result":[
						{"poll_type":"message",
							"value":{"msg_id":18012,"from_uin":2389297780,
						 			 "to_uin":1586030808,"msg_id2":578422,"msg_type":9,"reply_ip":176498388,
						    		 "time":1329017141,
						 			 "content":[["font",{"size":13,"color":"000000","style":[0,0,0],"name":"\u9ED1\u4F53"}],"9\u70B9 "]
									}
						}
					  ]
}
			 */
			bld.append("{\"retcode\":").append(this.retcode).append(",");
			if(this.errmsg != null)
				appendValue(bld, "errmsg",this.errmsg);//bld.append("\"errmsg\":").append(this.errmsg).append(",");
			if(this.p != null)
				appendValue(bld, "p", this.p);
			if(this.t != null)
				appendValue(bld, "t", this.t);
			
			if(this.result != null && this.result.size() > 0) {
				appendStr(bld, "result");
				bld.append(":[");
				//the results
				Poll2_ResponseBody_Result resultObj = null;
				for(int i = 0; i < this.result.size();i++) {
					resultObj = this.result.get(i);
					bld.append(resultObj.toString());
					if(i < this.result.size() - 1) {
						bld.append(",");
					}
				}
				
				
				bld.append("]}");
			}
			
			
			return bld.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"retcode\":102,\"errmsg\":\"" + e.getMessage() +  "\"}";
//			return e.toString();
		}
		
	}

	private void appendStr(StringBuilder bld, String string) {
		bld.append("\"").append(string).append("\"");
		
	}

	private void appendValue(StringBuilder bld, String key, String value) {
		bld.append("\"").append(key).append("\":");
		bld.append("\"").append(value).append("\"");
		bld.append(",");
	}

	public String getP() {
		return p;
	}

	public void setP(String p) {
		this.p = p;
	}

	public String getT() {
		return t;
	}

	public void setT(String t) {
		this.t = t;
	}

	
}
